package org.flik.client.login;

import org.flik.client.login.service.LoginServiceAsync;
import org.flik.client.shared.ClientUtils;
import org.flik.server.dto.DuplicateUserExcecption;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class NewAccountScreen extends Composite {

	private static NewAccountScreenUiBinder uiBinder = GWT.create(NewAccountScreenUiBinder.class);

	interface NewAccountScreenUiBinder extends UiBinder<Widget, NewAccountScreen> {
	}

	@UiField
	TextBox userName, firstName, lastName, email, password, verifyPassword; 
	
	@UiField
	Button create, cancel;
	
	@UiField 
	Label errorMsg, userNameLabel; 
	
	@UiField
	DeckPanel deckPanel; 
	
	public NewAccountScreen() {
		initWidget(uiBinder.createAndBindUi(this));
		deckPanel.showWidget(0);
	}
	
	public void reset() {
		deckPanel.showWidget(0);
		create.setVisible(true);
		userName.setValue("");
		firstName.setValue("");
		lastName.setValue("");
		email.setValue("");
		password.setValue("");
		verifyPassword.setValue("");
	}
	
	@UiHandler("create")
	public void onCreateButtonClick(ClickEvent clickEvent){
		
		if (isValid())
			LoginServiceAsync.INSTANCE.createUser(userName.getValue(), password.getValue(), firstName.getValue(), lastName.getValue(), email.getValue(), new AsyncCallback<Void>() {

				@Override
				public void onFailure(Throwable caught) {
					if (caught instanceof DuplicateUserExcecption){
						errorMsg.setText(caught.getMessage());
					}
				}

				@Override
				public void onSuccess(Void result) {
					deckPanel.showWidget(1);
					create.setVisible(false);
					userNameLabel.setText(userName.getText());
				}
			});
	}
	
	public void addCancelButtonClickHandler(ClickHandler handler){
		cancel.addClickHandler(handler);
//		addHandler(handler, ClickEvent.getType());
	}
	
	
	
	private boolean isValid() {
		String errorMsgText = 
			ClientUtils.isBlank(userName)?"invalid value in 'User Name' field":
				ClientUtils.isBlank(password)?"invalid value in 'Password' field":
					ClientUtils.isBlank(verifyPassword)?"invalid value in 'Verify Password' field":
						ClientUtils.isBlank(firstName)?"invalid value in 'First Name' field":
							ClientUtils.isValidEmail(email)?"Invalid email address":
								!ClientUtils.isEqual(password, verifyPassword)?"'Password' and 'Verify Password' fields must match":"";
		errorMsg.setText(errorMsgText);
		boolean valid = ClientUtils.isBlank(errorMsgText);
		return valid;
	}
}
